package com.smh.a01immutableDemo1;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ImmutableDemo4 {
    public static void main(String[] args) {

        HashMap<Integer,String> hm = new HashMap<>();
        hm.put(1,"a");
        hm.put(2,"b");
        hm.put(3,"c");
        hm.put(4,"d");
        hm.put(5,"e");
        hm.put(6,"f");
        hm.put(7,"g");
        hm.put(8,"h");
        hm.put(9,"i");
        hm.put(10,"j");
        hm.put(11,"k");
        hm.put(12,"l");
        System.out.println(hm);
        //获取键值对集合
        Set<Map.Entry<Integer, String>> entries = hm.entrySet();
        //将键值对集合转化为数组
        //如果集合数据大于数组长度,会创建新的数组,如果小于,则对于的会被赋为null
        //所以这里写0是为了不产生null,并且null的话无法调用方法,还会报错
        Map.Entry[] array = entries.toArray(new Map.Entry[0]);

//        for (Map.Entry entry : array) {
//            System.out.println(entry.getKey()+"="+entry.getValue());
//        }
        //让后赋值
        Map map = Map.ofEntries(array);
        System.out.println(map);

        //简化步骤
        Map<Object, Object> map1 = Map.ofEntries(hm.entrySet().toArray(new Map.Entry[0]));
        System.out.println(map1);

        //jdk10之后有一个方法
        //底层还是上面的方法
        Map<Integer, String> map2 = Map.copyOf(hm);
        System.out.println(map2);


    }
}
